package com.dhcc.cdc.busi.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.*;
import com.dhcc.cdc.busi.entity.*;
import com.dhcc.cdc.busi.service.ICdcDictionaryService;
import com.dhcc.cdc.busi.service.ICdcLocationService;
import com.dhcc.cdc.busi.service.ICdcSurgeryService;
import com.dhcc.cdc.busi.service.ICdcUserService;
import com.dhcc.cdc.sys.dao.CdcDataMapper;
import com.dhcc.cdc.sys.entity.CdcDataEntity;
import com.dhcc.cdc.sys.service.ICdcDataService;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 手术表数据服务层接口
 *
 * @ClassName: CdcSurgeryServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:06
 */
@Service("cdcSurgeryService")
public class CdcSurgeryServiceImpl extends BaseServiceImpl<CdcSurgeryMapper, CdcSurgeryEntity> implements ICdcSurgeryService {
	@Autowired
	ICdcDictionaryService dictritionSrv;
	@Autowired
	ICdcDataService dataSrv;
	@Autowired
	ICdcLocationService locSrv;
	@Autowired
	ICdcUserService userSrv;

	@Autowired
	private CdcEpisodeMapper cdcEpisodeMapper;

	@Autowired
	private CdcDictypeMapper cdcDictypeMapper;

	@Autowired
	private CdcDictionaryMapper cdcDictionaryMapper;

	@Autowired
	private CdcUserMapper cdcUserMapper;

	@Autowired
	private CdcDataMapper cdcDataMapper;

	@Autowired
	private CdcLocationMapper cdcLocationMapper;

	@Override
	public List<CdcSurgeryEntity> page(Page<CdcSurgeryEntity> page, Map<String, Object> map) {
		return warpSurgery(this.baseMapper.page(page, map));
	}
	@Override
	public List<CdcSurgeryEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public List<CdcSurgeryEntity> getFrontPageICDAll(Long aEpisodeID, String diagSource, List<String> diagType) {
		List<CdcSurgeryEntity> res = new ArrayList<>();
		Map<String, Object> map = new HashMap<>();
		map.put("episodeId", aEpisodeID);
		List<CdcSurgeryEntity> list = this.list(map);
		for (CdcSurgeryEntity cdcSurgeryEntity : list){
			CdcDictionaryEntity dictionaryEntity = dictritionSrv.selectById(cdcSurgeryEntity.getBusSurgSourceId());
			if (Objects.equals(diagSource, dictionaryEntity.getCode())){
				CdcDictionaryEntity cdcDictionaryEntity1 = dictritionSrv.selectById(cdcSurgeryEntity.getBusSurgTypeId());
				if (cdcDictionaryEntity1==null||diagType.contains(cdcDictionaryEntity1.getCode())){
					res.add(cdcSurgeryEntity);
				}
			}
		}
		return res;
	}

	public List<CdcSurgeryEntity> warpSurgery(List<CdcSurgeryEntity> list) {
		Map<Long, Object> cacheDic = new HashMap<Long, Object>();

		for (CdcSurgeryEntity surgery : list) {
			if (surgery.getBusSurgTypeId() != null) {
				if (cacheDic.containsKey(surgery.getBusSurgTypeId())) {
					surgery.setBusSurgType((CdcDictionaryEntity) cacheDic.get(surgery.getBusSurgTypeId()));
				} else {
					CdcDictionaryEntity SurgType = this.dictritionSrv.selectById(surgery.getBusSurgTypeId());
					surgery.setBusSurgType(SurgType);
					cacheDic.put(surgery.getBusSurgTypeId(), SurgType);
				}
			} else {
				surgery.setBusSurgType(new CdcDictionaryEntity());
			}
			if (surgery.getBusSurgStatusId() != null) {
				if (cacheDic.containsKey(surgery.getBusSurgStatusId())) {
					surgery.setBusSurgStatus((CdcDictionaryEntity) cacheDic.get(surgery.getBusSurgStatusId()));
				} else {
					CdcDictionaryEntity SurgStatus = this.dictritionSrv.selectById(surgery.getBusSurgStatusId());
					surgery.setBusSurgStatus(SurgStatus);
					cacheDic.put(surgery.getBusSurgStatusId(), SurgStatus);
				}
			} else {
				surgery.setBusSurgStatus(new CdcDictionaryEntity());
			}
			if (surgery.getSurgLocId() != null) {
				if (cacheDic.containsKey(surgery.getSurgLocId())) {
					surgery.setSurgLoc((CdcLocationEntity) cacheDic.get(surgery.getSurgLocId()));
				} else {
					CdcLocationEntity SurgLoc = this.locSrv.selectById(surgery.getSurgLocId());
					surgery.setSurgLoc(SurgLoc);
					cacheDic.put(surgery.getSurgLocId(), SurgLoc);
				}
			} else {
				surgery.setSurgLoc(new CdcLocationEntity());
			}
			if (surgery.getSurgDocId() != null) {
				if (cacheDic.containsKey(surgery.getSurgDocId())) {
					surgery.setSurgDoc((CdcUserEntity) cacheDic.get(surgery.getSurgDocId()));
				} else {
					CdcUserEntity SurgDoc = this.userSrv.selectById(surgery.getSurgDocId());
					surgery.setSurgDoc(SurgDoc);
					cacheDic.put(surgery.getSurgDocId(), SurgDoc);
				}
			} else {
				surgery.setSurgDoc(new CdcUserEntity());
			}

			if (surgery.getBusSurgInciId() != null) {
				if (cacheDic.containsKey(surgery.getBusSurgInciId())) {
					surgery.setBusSurgInci((CdcDictionaryEntity) cacheDic.get(surgery.getBusSurgInciId()));
				} else {
					CdcDictionaryEntity SurgInci = this.dictritionSrv.selectById(surgery.getBusSurgInciId());
					surgery.setBusSurgInci(SurgInci);
					cacheDic.put(surgery.getBusSurgInciId(), SurgInci);
				}
			} else {
				surgery.setBusSurgInci(new CdcDictionaryEntity());
			}
			if (surgery.getBusSurgHealId() != null) {
				if (cacheDic.containsKey(surgery.getBusSurgHealId())) {
					surgery.setBusSurgHeal((CdcDictionaryEntity) cacheDic.get(surgery.getBusSurgHealId()));
				} else {
					CdcDictionaryEntity SurgHeal = this.dictritionSrv.selectById(surgery.getBusSurgHealId());
					surgery.setBusSurgHeal(SurgHeal);
					cacheDic.put(surgery.getBusSurgHealId(), SurgHeal);
				}
			} else {
				surgery.setBusSurgHeal(new CdcDictionaryEntity());
			}
			if (surgery.getBusSurgAnesId() != null) {
				if (cacheDic.containsKey(surgery.getBusSurgAnesId())) {
					surgery.setBusSurgAnes((CdcDictionaryEntity) cacheDic.get(surgery.getBusSurgAnesId()));
				} else {
					CdcDictionaryEntity SurgAnes = this.dictritionSrv.selectById(surgery.getBusSurgAnesId());
					surgery.setBusSurgAnes(SurgAnes);
					cacheDic.put(surgery.getBusSurgAnesId(), SurgAnes);
				}
			} else {
				surgery.setBusSurgAnes(new CdcDictionaryEntity());
			}

			if (surgery.getDataId() != null) {
				if (cacheDic.containsKey(surgery.getDataId())) {
					surgery.setData((CdcDataEntity) cacheDic.get(surgery.getDataId()));
				} else {
					CdcDataEntity data = this.dataSrv.selectById(surgery.getDataId());
					surgery.setData(data);
					cacheDic.put(surgery.getDataId(), data);
				}
			} else {
				surgery.setData(new CdcDataEntity());
			}
		}
		return list;
	}
	@Override
	public String handleXmlData(String dataStr) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(dataStr); // 将字符串转为XML
			Element rootElt = doc.getRootElement(); // 获取根节点
			Iterator<Element> iter = rootElt.elementIterator("surgery"); // 获取根节点下的子节点surgery
			// 遍历surgery节点
			while (iter.hasNext()) {
				Element surEle = iter.next();
				String xcode = surEle.elementTextTrim("xcode"); //索引码
				String episodeid = surEle.elementTextTrim("episodeid"); //就诊记录
				String description = surEle.elementTextTrim("description"); //描述
				String code = surEle.elementTextTrim("code"); //编码
				String bussurgtypecode = surEle.elementTextTrim("bussurgtypecode"); //类型
				String bussurgtypedesc = surEle.elementTextTrim("bussurgtypedesc"); //类型
				String bussurgstatuscode = surEle.elementTextTrim("bussurgstatuscode"); //状态
				String bussurgstatusdesc = surEle.elementTextTrim("bussurgstatusdesc"); //状态
				String startdatestr = surEle.elementTextTrim("startdate"); //手术开始日期
				String enddatestr = surEle.elementTextTrim("enddate"); //手术结束日期
				String surglocid = surEle.elementTextTrim("surglocid"); //科室
				String surgdocid = surEle.elementTextTrim("surgdocid"); //主刀医生
				String assi1st = surEle.elementTextTrim("assi1st"); //一助
				String assi2nd = surEle.elementTextTrim("assi2nd"); //二助
				String bussurgincicode = surEle.elementTextTrim("bussurgincicode"); //切口等级
				String bussurgincidesc = surEle.elementTextTrim("bussurgincidesc"); //切口等级
				String bussurghealcode = surEle.elementTextTrim("bussurghealcode"); //愈合情况
				String bussurghealdesc = surEle.elementTextTrim("bussurghealdesc"); //愈合情况
				String bussurganescode = surEle.elementTextTrim("bussurganescode"); //麻醉方式
				String bussurganesdesc = surEle.elementTextTrim("bussurganesdesc"); //麻醉方式
				String anesdoc = surEle.elementTextTrim("anesdoc"); //麻醉医生
				String asascore = surEle.elementTextTrim("asascore"); //ASA评分
				String nnisgrade = surEle.elementTextTrim("nnisgrade"); //NNIS分级
				String wbccountstr = surEle.elementTextTrim("wbccount"); //术前外周WBC计数
				String incicountstr = surEle.elementTextTrim("incicount"); //切口个数
				String isendoscopestr = surEle.elementTextTrim("isendoscope"); //是否使用窥镜
				String isimplantsstr = surEle.elementTextTrim("isimplants"); //是否有植入物
				String bloodlose = surEle.elementTextTrim("bloodlose"); //失血量
				String bloodget = surEle.elementTextTrim("bloodget"); //输血量
				String complication = surEle.elementTextTrim("complication"); //术后并发症
				String bussurgsourcecode = surEle.elementTextTrim("bussurgsourcecode"); //手术数据来源
				String bussurgsourcedesc = surEle.elementTextTrim("bussurgsourcedesc"); //手术数据来源
				String updatetimestr= surEle.elementTextTrim("updatetime"); //更新时间
				String updateuserid= surEle.elementTextTrim("updateuserid"); //更新人
				String opercode= surEle.elementTextTrim("opercode"); //操作码
				CdcSurgeryEntity sur = new CdcSurgeryEntity();
				sur.setXcode(xcode);
				//获取dataid
				CdcDataEntity cdcDataEntity = new CdcDataEntity();
				cdcDataEntity.setCode("SurgeryData");
				cdcDataEntity.setIsActive(1);
				cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
				Long dataId = cdcDataEntity.getId();
				//获取basedataId
				CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
				cdcDataEntityBase.setCode("HisBase");
				cdcDataEntityBase.setIsActive(1);
				cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
				Long basedataId = cdcDataEntityBase.getId();
				if(CommonUtil.isNotEmpty(updatetimestr)) {
					Date updatetime = sdf.parse(updatetimestr);
					sur.setUpdateDate(updatetime);
				}
				//获取更新人
				if(CommonUtil.isNotEmpty(updateuserid)) {
					CdcUserEntity cdcUserEntity = new CdcUserEntity();
					cdcUserEntity.setXcode(updateuserid);
					cdcUserEntity.setDataId(basedataId);
					cdcUserEntity.setIsActive(1);
					cdcUserEntity = cdcUserMapper.selectOne(cdcUserEntity);
					//判断人员存在不存在
					Long updateuser = null;
					if(CommonUtil.isEmpty(cdcUserEntity)) {//不存在  保存到用户表里面
						cdcUserEntity = new CdcUserEntity();
						cdcUserEntity.setXcode(updateuserid);
						cdcUserEntity.setIsActive(1);
						cdcUserEntity.setCode("newuser"+updateuserid);
						cdcUserEntity.setName("newuser"+updateuserid);
						cdcUserEntity.setDataId(basedataId);
						//保存用户
						cdcUserMapper.insert(cdcUserEntity);
						updateuser = cdcUserEntity.getId();
					}else {
						updateuser = cdcUserEntity.getId();
					}
					sur.setUpdateBy(updateuser);
				}
				if("1".equals(opercode)) {//保存/修改
					//获取episodeid
					CdcEpisodeEntity cdcEpisodeEntity = new CdcEpisodeEntity();
					cdcEpisodeEntity.setXcode(episodeid);
					cdcEpisodeEntity=cdcEpisodeMapper.selectOne(cdcEpisodeEntity);//就诊信息没在库中？？？
					Long epsid = cdcEpisodeEntity.getId();
					sur.setEpisodeId(epsid);
					if(CommonUtil.isNotEmpty(description)) {
						sur.setDescription(description);
					}
					if(CommonUtil.isNotEmpty(code)) {
						sur.setCode(code);
					}
					//获取surgtypeId
					if(CommonUtil.isNotEmpty(bussurgtypecode)) {
						CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
						cdcDictypeEntity.setCode("bus_surg_type");
						cdcDictypeEntity.setIsActive(1);
						cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
						Long dicsurgtypeId = cdcDictypeEntity.getId();
						//如果没有对应ID就要插入
						CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
						cdcDictionaryEntity.setDictypeId(dicsurgtypeId);
						cdcDictionaryEntity.setCode(bussurgtypecode);
						cdcDictionaryEntity.setIsActive(1);
						cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
						//判断字典存在不存在
						Long busdiagcateid = null;
						if(CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
							cdcDictionaryEntity = new CdcDictionaryEntity();
							cdcDictionaryEntity.setDictypeId(dicsurgtypeId);
							cdcDictionaryEntity.setCode(bussurgtypecode);
							cdcDictionaryEntity.setDescription(bussurgtypedesc);
							cdcDictionaryEntity.setIsActive(1);
							//保存字典
							cdcDictionaryMapper.insert(cdcDictionaryEntity);
							busdiagcateid = cdcDictionaryEntity.getId();
						}else {
							busdiagcateid = cdcDictionaryEntity.getId();
						}
						sur.setBusSurgTypeId(busdiagcateid);
					}
					//获取DicSurgStatusId
					if(CommonUtil.isNotEmpty(bussurgstatuscode)) {
						CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
						cdcDictypeEntity.setCode("bus_surg_status");
						cdcDictypeEntity.setIsActive(1);
						cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
						Long dicsurgstatusId = cdcDictypeEntity.getId();
						//如果没有对应ID就要插入
						CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
						cdcDictionaryEntity.setDictypeId(dicsurgstatusId);
						cdcDictionaryEntity.setCode(bussurgstatuscode);
						cdcDictionaryEntity.setIsActive(1);
						cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
						//判断字典存在不存在
						Long bussurgstatusid = null;
						if(CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
							cdcDictionaryEntity = new CdcDictionaryEntity();
							cdcDictionaryEntity.setDictypeId(dicsurgstatusId);
							cdcDictionaryEntity.setCode(bussurgstatuscode);
							cdcDictionaryEntity.setDescription(bussurgstatusdesc);
							cdcDictionaryEntity.setIsActive(1);
							//保存字典
							cdcDictionaryMapper.insert(cdcDictionaryEntity);
							bussurgstatusid = cdcDictionaryEntity.getId();
						}else {
							bussurgstatusid = cdcDictionaryEntity.getId();
						}
						sur.setBusSurgStatusId(bussurgstatusid);
					}
					if(CommonUtil.isNotEmpty(startdatestr)) {
						Date startdate = sdf.parse(startdatestr);
						sur.setStartDate(startdate);
					}
					if(CommonUtil.isNotEmpty(enddatestr)) {
						Date enddate = sdf.parse(enddatestr);
						sur.setEndDate(enddate);
					}
					if(CommonUtil.isNotEmpty(surglocid)) {
						CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
						cdcLocationEntity.setXcode(surglocid);
						cdcLocationEntity.setDataId(basedataId);
						cdcLocationEntity.setIsActive(1);
						cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
						if(CommonUtil.isNotEmpty(cdcLocationEntity)) {
							Long surgloclongid = cdcLocationEntity.getId();
							sur.setSurgLocId(surgloclongid);
						}
					}
					//获取术者
					if(CommonUtil.isNotEmpty(surgdocid)) {
						CdcUserEntity cdcUserEntity = new CdcUserEntity();
						cdcUserEntity.setXcode(surgdocid);
						cdcUserEntity.setDataId(basedataId);
						cdcUserEntity.setIsActive(1);
						cdcUserEntity = cdcUserMapper.selectOne(cdcUserEntity);
						//判断人员存在不存在
						Long surgdoclongid = null;
						if(CommonUtil.isEmpty(cdcUserEntity)) {//不存在  保存到用户表里面
							cdcUserEntity = new CdcUserEntity();
							cdcUserEntity.setXcode(surgdocid);
							cdcUserEntity.setIsActive(1);
							cdcUserEntity.setCode("newuser"+surgdocid);
							cdcUserEntity.setName("newuser"+surgdocid);
							cdcUserEntity.setDataId(basedataId);
							//保存用户
							cdcUserMapper.insert(cdcUserEntity);
							surgdoclongid = cdcUserEntity.getId();
						}else {
							surgdoclongid = cdcUserEntity.getId();
						}
						sur.setSurgDocId(surgdoclongid);
					}
					//获取一助
					if(CommonUtil.isNotEmpty(assi1st)) {
						sur.setAssi1st(assi1st);
					}
					//获取术者
					if(CommonUtil.isNotEmpty(assi2nd)) {
						sur.setAssi2nd(assi2nd);
					}
					//获取SurgInciId
					if(CommonUtil.isNotEmpty(bussurgincicode)) {
						CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
						cdcDictypeEntity.setCode("bus_surg_inci");
						cdcDictypeEntity.setIsActive(1);
						cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
						Long dicsurginciId = cdcDictypeEntity.getId();
						//如果没有对应ID就要插入
						CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
						cdcDictionaryEntity.setDictypeId(dicsurginciId);
						cdcDictionaryEntity.setCode(bussurgincicode);
						cdcDictionaryEntity.setIsActive(1);
						cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
						//判断字典存在不存在
						Long bussurginciid = null;
						if(CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
							cdcDictionaryEntity = new CdcDictionaryEntity();
							cdcDictionaryEntity.setDictypeId(dicsurginciId);
							cdcDictionaryEntity.setCode(bussurgincicode);
							cdcDictionaryEntity.setDescription(bussurgincidesc);
							cdcDictionaryEntity.setIsActive(1);
							//保存字典
							cdcDictionaryMapper.insert(cdcDictionaryEntity);
							bussurginciid = cdcDictionaryEntity.getId();
						}else {
							bussurginciid = cdcDictionaryEntity.getId();
						}
						sur.setBusSurgInciId(bussurginciid);
					}
					//获取surghealId
					if(CommonUtil.isNotEmpty(bussurghealcode)) {
						CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
						cdcDictypeEntity.setCode("bus_surg_heal");
						cdcDictypeEntity.setIsActive(1);
						cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
						Long dicsurghealId = cdcDictypeEntity.getId();
						//如果没有对应ID就要插入
						CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
						cdcDictionaryEntity.setDictypeId(dicsurghealId);
						cdcDictionaryEntity.setCode(bussurghealcode);
						cdcDictionaryEntity.setIsActive(1);
						cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
						//判断字典存在不存在
						Long bussurghealid = null;
						if(CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
							cdcDictionaryEntity = new CdcDictionaryEntity();
							cdcDictionaryEntity.setDictypeId(dicsurghealId);
							cdcDictionaryEntity.setCode(bussurghealcode);
							cdcDictionaryEntity.setDescription(bussurghealdesc);
							cdcDictionaryEntity.setIsActive(1);
							//保存字典
							cdcDictionaryMapper.insert(cdcDictionaryEntity);
							bussurghealid = cdcDictionaryEntity.getId();
						}else {
							bussurghealid = cdcDictionaryEntity.getId();
						}
						sur.setBusSurgHealId(bussurghealid);
					}
					//获取surganesId
					if(CommonUtil.isNotEmpty(bussurganescode)) {
						CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
						cdcDictypeEntity.setCode("bus_surg_anes");
						cdcDictypeEntity.setIsActive(1);
						cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
						Long dicsurganesId = cdcDictypeEntity.getId();
						//如果没有对应ID就要插入
						CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
						cdcDictionaryEntity.setDictypeId(dicsurganesId);
						cdcDictionaryEntity.setCode(bussurganescode);
						cdcDictionaryEntity.setIsActive(1);
						cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
						//判断字典存在不存在
						Long bussurganesid = null;
						if(CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
							cdcDictionaryEntity = new CdcDictionaryEntity();
							cdcDictionaryEntity.setDictypeId(dicsurganesId);
							cdcDictionaryEntity.setCode(bussurganescode);
							cdcDictionaryEntity.setDescription(bussurganesdesc);
							cdcDictionaryEntity.setIsActive(1);
							//保存字典
							cdcDictionaryMapper.insert(cdcDictionaryEntity);
							bussurganesid = cdcDictionaryEntity.getId();
						}else {
							bussurganesid = cdcDictionaryEntity.getId();
						}
						sur.setBusSurgAnesId(bussurganesid);
					}
					//获取麻醉医师
					if(CommonUtil.isNotEmpty(anesdoc)) {
						sur.setAnesDoc(anesdoc);
					}
					if(CommonUtil.isNotEmpty(asascore)) {
						sur.setAsaScore(asascore);
					}
					if(CommonUtil.isNotEmpty(nnisgrade)) {
						sur.setNnisGrade(nnisgrade);
					}
					if(CommonUtil.isNotEmpty(wbccountstr)) {
						Integer wbccount = Integer.valueOf(wbccountstr);
						sur.setWbcCount(wbccount);
					}
					if(CommonUtil.isNotEmpty(incicountstr)) {
						Integer incicount = Integer.valueOf(incicountstr);
						sur.setInciCount(incicount);
					}
					if(CommonUtil.isNotEmpty(isendoscopestr)) {
						Integer isendoscope = Integer.valueOf(isendoscopestr);
						sur.setIsEndoscope(isendoscope);
					}
					if(CommonUtil.isNotEmpty(isimplantsstr)) {
						Integer isimplants = Integer.valueOf(isimplantsstr);
						sur.setIsImplants(isimplants);
					}
					if(CommonUtil.isNotEmpty(bloodlose)) {
						sur.setBloodLose(bloodlose);
					}
					if(CommonUtil.isNotEmpty(bloodget)) {
						sur.setBloodGet(bloodget);
					}
					if(CommonUtil.isNotEmpty(complication)) {
						sur.setComplication(complication);
					}
					//获取surgsourceId
					if(CommonUtil.isNotEmpty(bussurgsourcecode)) {
						CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
						cdcDictypeEntity.setCode("bus_surg_source");
						cdcDictypeEntity.setIsActive(1);
						cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
						Long dicsurgsourceId = cdcDictypeEntity.getId();
						//如果没有对应ID就要插入
						CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
						cdcDictionaryEntity.setDictypeId(dicsurgsourceId);
						cdcDictionaryEntity.setCode(bussurgsourcecode);
						cdcDictionaryEntity.setIsActive(1);
						cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
						//判断字典存在不存在
						Long bussurgsourceid = null;
						if(CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
							cdcDictionaryEntity = new CdcDictionaryEntity();
							cdcDictionaryEntity.setDictypeId(dicsurgsourceId);
							cdcDictionaryEntity.setCode(bussurgsourcecode);
							cdcDictionaryEntity.setDescription(bussurgsourcedesc);
							cdcDictionaryEntity.setIsActive(1);
							//保存字典
							cdcDictionaryMapper.insert(cdcDictionaryEntity);
							bussurgsourceid = cdcDictionaryEntity.getId();
						}else {
							bussurgsourceid = cdcDictionaryEntity.getId();
						}
						sur.setBusSurgSourceId(bussurgsourceid);
					}
					sur.setDataId(dataId);
					sur.setIsActive(1);
				}else {
					sur.setIsActive(0);
				}
				//先判断是不是有该对象
				Wrapper<CdcSurgeryEntity> wrapper =  new EntityWrapper<CdcSurgeryEntity>();
				wrapper.eq("xcode", sur.getXcode());
				wrapper.eq("data_id", dataId);
				int count = this.selectCount(wrapper);
				if(count>0) {
					this.update(sur, wrapper);
				}else {
					this.insert(sur);
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
			return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
		} catch (Exception e) {
			e.printStackTrace();
			return "<response><status>0</status><message>"+e.getMessage()+"</message></response>";
		}
		return "<response><status>1</status><message>ok</message></response>";
	}

}